package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.NewsQuery;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("news")
public class NewsController {
    @Autowired
    private INewsService newsService;

    //待审核日报列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NewsQuery qo){
        //状态必须是待审核
        qo.setState(News.STATE_WAITING);

        //按创建时间倒序排序
        qo.setOrderBy("createTime desc");
        model.addAttribute("result",newsService.query(qo));
        return "news/list";
    }

    //修改日报状态
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(News news){

        JSONResult result = new JSONResult();
        try {
            newsService.changeState(news);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //获取日报文章的详细内容
    @RequestMapping("/getNewsContent")
    @ResponseBody
    public Object getNewsContent(Long newsId){
        return newsService.getNewsContentById(newsId);
    }

    //已发布游记列表
    @RequestMapping("/releaseList")
    public String releaseList(Model model, @ModelAttribute("qo") NewsQuery qo){
        //状态必须是已发布
        qo.setState(Travel.STATE_RELEASE);

        //按发布时间倒序排序
        qo.setOrderBy("releaseTime desc");
        model.addAttribute("result",newsService.query(qo));
        return "news/releaseList";
    }
}
